package com.showcal.service.listener;

import com.showcal.framework.cache.CacheManager;
import com.showcal.platform.biz.SettingKeywordManager;
import com.showcal.platform.domain.SettingKeyword;
import com.showcal.platform.request.SettingKeywordGetRequest;
import com.showcal.platform.response.SettingKeywordGetResponse;
import com.showcal.service.event.IMMessageSendEvent;
import com.showcal.service.event.bean.IMMessagBean;
import com.showcal.service.po.QuestionPO;
import com.xiniunet.framework.security.Passport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 添加消息缓存
 * Created by guzhixiong on 2015/11/17.
 */
@Component
public class AddQuestionCacheListener  implements ApplicationListener<IMMessageSendEvent> {
    @Autowired
    private SettingKeywordManager settingKeywordManager;
    private static final String CACHEQUESTION = "CACHEQUESTION";
    private static final String CACHEKEYWORDQUESTION = "CACHEKEYWORDQUESTION";
    private static final String CACHEOTHERKEYWORDQUESTION = "CACHEOTHERKEYWORDQUESTION";
    private static final String CACHENAME = "SERQUESTION";
    private static final Integer LEVEL = 2;
    @Async
    @Override
    public void onApplicationEvent(IMMessageSendEvent imMessageSendEvent) {
        System.out.println("添加消息缓存............");
        IMMessagBean imMessagBean = (IMMessagBean) imMessageSendEvent.getSource();

        QuestionPO questionPO =imMessagBean.getQuestion();
        Passport passport = imMessagBean.getPassport();
        CacheManager.set(LEVEL, CACHENAME, passport.getUserId() + "_" + questionPO.getAnswerUserId(), questionPO);
        CacheManager.set(LEVEL, CACHEQUESTION, questionPO.getId(), questionPO);
        Long questionId = questionPO.getId();
        Long showcalId = questionPO.getAnswerId();
        // 获取对应瘦咖缓存数据
        List<Long> otherQuestionIds = CacheManager.get(LEVEL, List.class, CACHEOTHERKEYWORDQUESTION, showcalId);
        if (otherQuestionIds != null && otherQuestionIds.contains(questionId)) {
            if (questionPO.getKeyword() != null && questionPO.getKeyword() > 0) {
                // 添加至对应的keyword
                SettingKeywordGetRequest settingKeywordGetRequest = new SettingKeywordGetRequest();
                settingKeywordGetRequest.setId(questionPO.getKeyword());
                SettingKeywordGetResponse response = settingKeywordManager.get(settingKeywordGetRequest, passport);
                SettingKeyword settingKeyword = response.getSettingKeyword();
                if (settingKeyword != null) {
                    // 删除other
                    otherQuestionIds.remove(questionPO.getId());
                    CacheManager.set(LEVEL, CACHEOTHERKEYWORDQUESTION, showcalId, otherQuestionIds);
                    Map<String, Integer> keywordQuestion = CacheManager.get(LEVEL, Map.class, CACHEKEYWORDQUESTION, showcalId);
                    Integer count = keywordQuestion.get(settingKeyword.getId() + "&&" + settingKeyword.getKeyword());
                    count = count==null?0:count;
//                    List<Long> keywordquestions = keywordQuestion.get(settingKeyword.getId() + "&&" + settingKeyword.getKeyword());
//                    if (keywordquestions != null && !keywordquestions.isEmpty()) {
//                        keywordquestions.add(questionId);
//                    } else {
//                        keywordquestions = new ArrayList<>();
//                        keywordquestions.add(questionId);
//                    }
                    count++;
                    keywordQuestion.put(settingKeyword.getId() + "&&" + settingKeyword.getKeyword(), count);
                    CacheManager.set(LEVEL, CACHEKEYWORDQUESTION, showcalId, keywordQuestion);
                }
            }
        } else {
            if (questionPO.getKeyword() != null && questionPO.getKeyword() > 0) {
                SettingKeywordGetRequest settingKeywordGetRequest = new SettingKeywordGetRequest();
                settingKeywordGetRequest.setId(questionPO.getKeyword());
                SettingKeywordGetResponse response = settingKeywordManager.get(settingKeywordGetRequest, passport);
                SettingKeyword settingKeyword = response.getSettingKeyword();
                if (settingKeyword != null) {
                    Map<String, Integer> keywordQuestion = CacheManager.get(LEVEL, Map.class, CACHEKEYWORDQUESTION, showcalId);
                    Integer count = keywordQuestion.get(settingKeyword.getId() + "&&" + settingKeyword.getKeyword());
                    count = count==null?0:count;
//                    if (keywordquestions != null && !keywordquestions.isEmpty()) {
//                        keywordquestions.add(questionId);
//                    } else {
//                        keywordquestions = new ArrayList<>();
//                        keywordquestions.add(questionId);
//                    }
                    count++;
                    keywordQuestion.put(settingKeyword.getId() + "&&" + settingKeyword.getKeyword(), count);
                    CacheManager.set(LEVEL, CACHEKEYWORDQUESTION, showcalId, keywordQuestion);
                } else {
                    otherQuestionIds.add(questionId);
                    CacheManager.set(LEVEL, CACHEOTHERKEYWORDQUESTION, showcalId, otherQuestionIds);
                }
            } else {
                otherQuestionIds = new ArrayList<>();
                otherQuestionIds.add(questionId);
                CacheManager.set(LEVEL, CACHEOTHERKEYWORDQUESTION, showcalId, otherQuestionIds);
            }
        }
    }
}
